package com.tongren.framework.sample;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.model.Response;
import com.tongren.framework.R;
import com.tongren.framework.callback.JsonCallback;
import com.tongren.framework.core.BaseListActivity;
import com.tongren.framework.core.CustomApplication;
import com.tongren.framework.me.activity.MyClassWorkListActivity;
import com.tongren.framework.me.activity.WorkInfoActivity;
import com.tongren.framework.model.BaseCourseware;
import com.tongren.framework.model.BasePageModel;
import com.tongren.framework.model.BaseResult;
import com.tongren.framework.model.BaseUser;
import com.tongren.framework.utils.DateHelper;
import com.tongren.framework.widgets.pull.BaseViewHolder;
import com.tongren.framework.widgets.pull.PullRecycler;

import java.util.ArrayList;

import static com.tongren.framework.constants.ConstantValues.BASE_IMG_URL;
import static com.tongren.framework.constants.ConstantValues.BASE_URL;

/**
 * zhangxu
 */
public class CoursewareListActivity extends BaseListActivity<BaseCourseware> {

    private int page = 1;
    private boolean isInitCache = false;

    @Override
    protected void setUpTitle(int titleResId) {
        super.setUpTitle(R.string.title_courseware_activity);
    }

    @Override
    protected void setUpData(Bundle savedInstanceState) {
        super.setUpData(savedInstanceState);
        recycler.setRefreshing();
    }

    @Override
    protected BaseViewHolder getViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.news_list_item, parent, false);
        return new SampleViewHolder(view);
    }



    @Override
    public void onRefresh(final int action) {
        if (mDataList == null) {
            mDataList = new ArrayList<>();
        }

        if (action == PullRecycler.ACTION_PULL_TO_REFRESH) {
            page = 1;
        }
        BaseUser user = CustomApplication.getInstance().getUserModel();
        OkGo.<BaseResult<BasePageModel<ArrayList<BaseCourseware>>>>post(BASE_URL+"searchDocList.ajax")//请求方式和请求url
                .tag(this)//请求的 tag, 主要用于取消对应的请求
//                .params("xlh", user.getXlh())//
                .params("editionPath", "1")//测试数据
                .params("currentPage", page++)//
                .params("maxRows", 10)//
                .cacheMode(CacheMode.FIRST_CACHE_THEN_REQUEST)  //缓存模式先使用缓存,然后使用网络数据
                .execute(new JsonCallback<BaseResult<BasePageModel<ArrayList<BaseCourseware>>>>() {
                    @Override
                    public void onSuccess(Response<BaseResult<BasePageModel<ArrayList<BaseCourseware>>>> response) {
//                        handleResponse(response);
                        String str = response.toString();
                        System.out.println(str);
                        if (action == PullRecycler.ACTION_PULL_TO_REFRESH) {
                            mDataList.clear();
                        }
                        if (response.body().result.getQueryList() == null || response.body().result.getQueryList().size() == 0) {
                            recycler.enableLoadMore(false);
                        } else {
                            recycler.enableLoadMore(true);
                            mDataList.addAll(response.body().result.getQueryList());
                            adapter.notifyDataSetChanged();
                        }
                        recycler.onRefreshCompleted();
                    }

                    @Override
                    public void onCacheSuccess(Response<BaseResult<BasePageModel<ArrayList<BaseCourseware>>>> response) {
                        //一般来说,只需呀第一次初始化界面的时候需要使用缓存刷新界面,以后不需要,所以用一个变量标识
                        if (!isInitCache) {
                            //一般来说,缓存回调成功和网络回调成功做的事情是一样的,所以这里直接回调onSuccess
                            onSuccess(response);
                            isInitCache = true;
                        }
                    }

                    @Override
                    public void onError(Response<BaseResult<BasePageModel<ArrayList<BaseCourseware>>>> response) {
//                        handleError(response);
                        recycler.onRefreshCompleted();
                    }
                });

    }

    class SampleViewHolder extends BaseViewHolder {

        ImageView iv_new_img;
        TextView tv_title;
        TextView tv_date;
        Button btn_down;

        public SampleViewHolder(View itemView) {
            super(itemView);
            tv_title = (TextView) itemView.findViewById(R.id.tv_new_title);
            tv_date = (TextView) itemView.findViewById(R.id.tv_new_date);
            iv_new_img = (ImageView) itemView.findViewById(R.id.iv_new_img);
            btn_down = (Button) itemView.findViewById(R.id.btn_down);
        }

        @Override
        public void onBindViewHolder(int position) {
            Glide.with(iv_new_img.getContext())
                    .load(BASE_IMG_URL + mDataList.get(position).getPhoto())
                    .centerCrop()
                    .placeholder(R.color.app_primary_color)
                    .crossFade()
                    .into(iv_new_img);
            tv_title.setText(mDataList.get(position).getDocumentName());
            String date = DateHelper.timeStamp2Date(mDataList.get(position).getCreateDate(),"yyyy-MM-dd");
            tv_date.setText(date);

        }

        @Override
        public void onItemClick(View view, int position) {
            Intent intent3 = new Intent(CoursewareListActivity.this, WorkInfoActivity.class);
            intent3.putExtra("title",mDataList.get(position).getDocumentName());
            intent3.putExtra("desp",mDataList.get(position).getDesp());
            intent3.putExtra("fileType",mDataList.get(position).getFileType());
            intent3.putExtra("createName",mDataList.get(position).getCreateName());
            intent3.putExtra("realUrl",mDataList.get(position).getRealUrl());
            intent3.putExtra("url",mDataList.get(position).getUrl());
            String date = DateHelper.timeStamp2Date(mDataList.get(position).getCreateDate(),"yyyy-MM-dd");
            intent3.putExtra("date",date);
            startActivity(intent3);
        }

    }
}
